function announcementGrid()
{
    $('#announcementGrid').jqGrid({
                url: siteUrl + 'announcement/get_announcements',
                mtype: 'post',
                datatype: 'json',
                colNames: ['ID', 'Title','Author', 'Date Posted', 'Operations'],
                colModel: [
                {name: 'announcement_id', index: 'announcement_id', hidden: true, key: true},
                {name: 'title', index: 'title'},
                {name: 'author', index: 'author'},
                {name: 'date_posted', index: 'date_posted'},
                {name: 'ops', index: 'ops', align: 'center', sortable: false, formatter: announcementOperations}
                ],
            autowidth: true,
            height: '200',
            pager: $('#pagination'),
            sortname: 'announcement_id',
            sortorder: 'asc',
            viewrecords: true,
            rownumbers: true,
            ondblClickRow: function(rowId, iRow, iCol, e) {
                window.location = siteUrl + 'announcement/view/' + rowId;
            }
    });
}

function announcementOperations(cell, opts, rowObj)
{
    var id = opts.rowId;
    
    var edit = '<a href="' + siteUrl + 'announcement/edit/' + id + '" title="Update" class="icon icon-update"></a>';
    edit = (user_type != 'admin') ? '<span class = "icon" ></span>' : edit;
    var del = '<a href="javascript:$.confirmDelete(' + id + ', \'deleteAnnouncement\', \'Announcement - Delete Record\')" title="Delete" class="icon icon-delete"></a>';
    del = (user_type != 'admin') ? '<span class = "icon" ></span>' : del;
    var view = '<a href="' + siteUrl + 'announcement/view/' + id + '" title="View" class="icon icon-search"></a>'
    view = (user_type == 'admin') ? '<span class = "icon" ></span>' : view;

    return edit+del+view;
}

function announcementRules()
{
    $('#announcement_form').validate({
        debug: true,
        rules: {
            title: {
                required: true
            },
            description: {
                required: true
            },
            file_name: {
                required: function() {
                    return false;
                    //return ( $('#announcement_id').val() == 0)
                }
            }
        },
        messages: {
            title: {
                required: $.Msgs('required')
            },
            description: {
                required: $.Msgs('required')
            },
            file_name: {
                required: $.Msgs('required')
            }
        },
        onkeyup: false,
        focusInvalid: true,
        errorElement: 'span',
        errorPlacement: function(error, element) {
            $(element).next('.icon-error').remove();
            $(element).parent().append('<span class="icon icon-error"></span>');
            var icon = $(element).next('span');

            icon.qtip({
                content: $(error).text(),
                position: {
                    corner: {
                        target: 'rightTop',
                        tooltip: 'leftBottom'
                    }
                },
                hide: {
                    when: 'inactive',
                    delay: 1500
                },
                style: 'fielderror'
            });
        },
        highlight: function(element, errorClass) {
            $(element).parent().addClass('ui-state-error ui-corner-all');
        },
        unhighlight: function(element, errorClass) {
            $(element).next('.icon-error').remove();
            $(element).parent().removeClass('ui-state-error ui-corner-all');
        },
        submitHandler: function(form) {
            $.loading('Saving Record');
            form.submit();
            //saveAnnouncement();
        }
    });

}

function saveAnnouncement(){
    $.loading('Saving Record');
    
    $.ajax({
        url: siteUrl + 'announcement/submit',
        type: 'post',
        data: $('#announcement_form').serialize(),
        success: function(response) {
            if (response)
            {
               window.location = siteUrl + 'announcement';
            }
        }
    });
}

function deleteAnnouncement(id)
{
    $.loading('Deleting Record');

    $.ajax({
        url: siteUrl + 'announcement/delete/' + id,
        type: 'post',
        dataType: 'json',
        success: function(response) {
            if (response == true)
            {
                window.location = siteUrl + 'announcement';
            }

        }
    });
}